<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="单位名称" prop="departmentId" class="dislabel">
        <el-cascader v-model="pram.departmentId" :disabled="pram.departmentId ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />
        <!-- allTreeList or parentOptions-->
      </el-form-item>
      <el-form-item label="职位名称" prop="position" class="dislabel">
        <el-input v-model="pram.position" maxlength="100" placeholder="职位名称"/>
      </el-form-item>
      <el-form-item label="是否主职" prop="isMajor">
        <el-radio-group v-model="pram.isMajor">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:person:update','admin:person:save']">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {Debounce} from '@/utils/validate'
import * as selfUtil from "@/utils/ZBKJIutil";
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    editKey: {
      type: Number,
      required: true,
      default: -1,
    },
    allParentOptions: {
      type: Array
    }
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        departmentId: null,
        position: null,
        isMajor: 0,

        depName:'',
        isMajorText:'',
      },
      rules: {
        departmentId: [{ required: true, message: '请选择单位名称', trigger: ['blur', 'change'] }],
        position: [{ required: true, message: '请填职位名称', trigger: ['blur', 'change'] }],
        isMajor: [{ required: true, message: '请选择是否主职', trigger: ['blur', 'change'] }],
      },

      parentOptions: [],
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideInfoDialog')
    },

    initEditData() {
      this.parentOptions = this.allParentOptions
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        this.handlerSave()
      })
    }),
    handlerSave() {
      const treeList = this.parentOptions
      this.pram.depName = this.getTreeName(treeList,this.pram.departmentId)
      /*treeList.forEach((item)=>{
        if(item.id === this.pram.departmentId) this.pram.departmentName = item.name
      })*/
      this.pram.isMajorText = this.pram.isMajor===1?"是":"否"
      this.$emit('assignInfoSaveAndHideInfoDialog',this.pram)
    },
    //树形数组递归通过id获取name
    getTreeName(list,id){
      for (let i = 0; i < list.length; i++) {
        let a=list[i]
        if(a.id===id){
          return a.name
        }else{
          if(a.child && a.child .length>0){
            let res=this.getTreeName(a.child ,id)
            if(res){
              return res
            }
          }
        }
      }
    },
    //处理哪一级别禁用
    addTreeListLabelForCasCard(arr, child) {
        arr.forEach((o,i) => {
          if (o.child && o.child.length) {
            o.disabled = true
            o.child.forEach((j) => {
              //j.disabled = true
              if (j.child && j.child.length) {
                j.child.forEach((k) => {
                  //k.disabled = true
                })
              }
            })
          }
        })
    },
  }
}
</script>

<style scoped lang="scss">
  .dislabel{
    ::v-deep.el-input--small .el-input__inner,
    ::v-deep.el-input--medium .el-input__inner{
      height:36px;
      line-height:36px;
      font-size:12px;
    }
    ::v-deep.el-textarea__inner{
      font-size:12px;
    }

  }
</style>
